import axios from "axios";
import { ElMessage } from "element-plus";

const instance = axios.create({
  baseURL: "http://localhost:8080",
  headers: {
    "Content-Type": "application/json",
    Authorization: `Bearer ${import.meta.env.VITE_OPENAI_API_KEY}`,
  },
});

instance.interceptors.response.use((response) => {
  if (response.status == 401) {
    ElMessage.error("您的登录信息失效，请重新登录");
  }
  if (!response.data.success) {
    response.data?.message && ElMessage.error(response.data.message);
    return Promise.reject(response);
  }
  return response;
});

export default instance;
